#! /bin/sh
function signApk
{
    echo "=========Start sign apk ### $1 ### with keystore. "
	jarsigner -verbose -sigalg SHA1withRSA -digestalg SHA1 -keystore ./A_KEYSTORE.keystore -storepass PASSWORD $1 ALIAS 
	echo "=========Start zipalign apk ."
	./zipalign -f -v 4 $1 ./release/$2_realease.apk
	# echo "=========Start check zipalign apk ."
	# zipalign -c -v 4 ./release/$2_realease.apk
	# echo "=========install release APK to device"
	# adb install -r pandabus_release.apk
	# echo "=========start Pandabus on device ==="
	# adb shell am start -n PACKAGE_ID/PACKAGE_ID.A_ACTIVITY
}
# get this script path
# filepath=$(cd "$(dirname "$0")"; pwd)
# go into the script folder
cd `dirname $0`
# check command jarsigner  exsit
command -v jarsigner >/dev/null 2>&1 || { echo >&2 "I require jarsigner but it's not found.  Aborting."; exit 1; }
# make a folder to storage apk after sign and zipalign
mkdir ./release
for entry in ./*
do
  	echo "$entry"
  	file=$entry 
# echo “filename: ${file%.*}”
	extension=${file##*.} 
	echo "$extension"
if [ "$extension" = "apk" ]; then  
	echo "is a APK"
	fileName=${file%.*}
	isRelease=${fileName#./FILE_NAME*_} 
	if [ "$isRelease" = "release" ]; then  
	    echo "is a release version ,so ignore"  
	else  
	    echo "not a release version,start sign..."  
	    signApk $file $fileName;
	fi   
    
else  
    echo "not a apk file, ignore"  
fi 
done
 echo "sign all apk file successfully!"  
